<%--                                                                       --%>
<%-- Copyright (c) 2004-2005 BroadVision, Inc.  All rights reserved.       --%>
<%--                                                                       --%>
<%-- This software is copyrighted. Under the copyright laws, this software --%>
<%-- may not be copied, in whole or in part, without prior written consent --%>
<%-- of BroadVision, Inc. or its assignees. This software is provided      --%>
<%-- under the terms of a license between BroadVision and the recipient,   --%>
<%-- and its use is subject to the terms of that license.                  --%>
<%--                                                                       --%>
<%-- This software may be protected by one or more U.S. and International  --%>
<%-- patents.  Certain applications of BroadVision One-To-One software are --%>
<%-- covered by U.S. patent 5,710,887.                                     --%>
<%--                                                                       --%>
<%-- TRADEMARKS: BroadVision and BroadVision One-To-One are registered     --%>
<%-- trademarks of BroadVision, Inc., in the United States and the         --%>
<%-- European Community, and are trademarks of BroadVision, Inc., in other --%>
<%-- countries.  The BroadVision logo, is a trademark of BroadVision,      --%>
<%-- Inc., in the United States and other countries. Additionally, IONA    --%>
<%-- and Orbix are trademarks of IONA Technologies, Ltd.  RSA, MD5, and    --%>
<%-- RC2 are trademarks of RSA Data Security, Inc.                         --%>
<%--                                                                       --%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<%@ page import="java.util.List" %>
<%@ page import="com.broadvision.emarketing.criteriabuilder.Tree" %>
<%@ page import="com.broadvision.emarketing.criteriabuilder.MvRowConditionBranch" %>
<%@ page import="com.broadvision.emarketing.action.Constants"%>

<%!
  private static final String INDENT = "&nbsp;&nbsp;&nbsp;&nbsp;";
%>

<%
  Tree tree = (Tree)session.getAttribute(Constants.CRITERIA_TREE);
  List nodeList = tree.getPreorderNodeList(true, false);
  pageContext.setAttribute("nodeList", nodeList);

%>


<c:set var="addImage">
  <html:img page="/mc/images/action/icon_add.gif" styleClass="actionIcon" 
    width="15px" height="15" border="0" 
    altKey="emarketing.criteriaBuilder.image.altKey.addCondition" 
    titleKey="emarketing.criteriaBuilder.image.altKey.addCondition" bundle="em" />
</c:set>
<c:set var="createNewImage">
  <html:img page="/mc/images/action/icon_new.gif" styleClass="actionIcon" 
    width="15px" height="15" border="0" 
    altKey="emarketing.criteriaBuilder.image.altKey.createNewConditionList"
    titleKey="emarketing.criteriaBuilder.image.altKey.createNewConditionList" bundle="em" />
</c:set>
<c:set var="editImage">
  <html:img page="/mc/images/action/icon_edit.gif" styleClass="actionIcon" 
    width="15px" height="15" border="0" 
    altKey="emarketing.criteriaBuilder.image.altKey.edit" 
    titleKey="emarketing.criteriaBuilder.image.altKey.edit" bundle="em" />
</c:set>

<c:set var="transparentImage">
  <html:img page="/mc/images/build/transparent.gif" 
    width="33px" height="15px" border="0" />
</c:set>
<c:set var="transparentImageHalfWidth">
  <html:img page="/mc/images/build/transparent.gif" 
    width="16px" height="15px" border="0" />
</c:set>
<c:set var="deleteImage">
  <html:img page="/mc/images/action/icon_delete.gif" styleClass="actionIcon" 
    width="15px" height="15" border="0" 
    altKey="emarketing.criteriaBuilder.image.altKey.delete"
    titleKey="emarketing.criteriaBuilder.image.altKey.delete" bundle="em" />
</c:set>

<c:set var="collapseImage">
  <html:img bundle="em" page="/mc/images/icons/icon_collapse.gif" width="15px" height="15" border="0" titleKey="emarketing.criteriaBuilder.image.altKey.collapseConditionList" altKey="emarketing.criteriaBuilder.image.altKey.collapseConditionList" />
</c:set>
<c:set var="expandImage">
  <html:img bundle="em" page="/mc/images/icons/icon_expand.gif" width="15px" height="15" border="0" titleKey="emarketing.criteriaBuilder.image.altKey.expandConditionList" altKey="emarketing.criteriaBuilder.image.altKey.expandConditionList" />
</c:set>



<script language="JavaScript">

  function doActionFinish(myform){  
    var actionStr;
    if( myform.fromType.value == "ruleset") {
      actionStr = '<html:rewrite page="/em/ruleset/ruleEdit.do" />';
    }
    else if( myform.fromType.value == "segment") {
      actionStr = '<html:rewrite page="/em/segment/segmentModRefresh.do" />';
    }       
    myform.action=actionStr;
    myform.submit();
    return true;
}

  function DeleteCondition(url) {
    answer=window.confirm("<bean:message bundle="em" key="emarketing.criteriaBuilder.javaScript.alert.confirmDeleteThisCondition"/>");
    if (answer == true){
       document.location.href = url;
    }
  }
  
  function launchConditionBrowser(id)
  {
      var str = "<html:rewrite page="/em/cb/attributeBrowse.do" />";
      str += "?key=" + id + "&actType=create&fromType=<c:out value="${emRuleCriteriaForm.map.fromType}" />";
          //alert(str);
            var qualifier=window.open(str,"Condition",
            "directories=no,width=450,height=550,left=300,top=100,resizable=yes,scrollbars=yes,location=no,menubar=no,status=no,toolbar=no,");
            if (qualifier != null){
              if (qualifier.opener == null)
                    qualifier.opener = self;
              qualifier.focus();
            }
  }
</script>


<!--start zone 5 content area-->
<table class="zoneFive" width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" width="100%">
      <div class="pageTitle">
        <bean:message bundle="em" key="emarketing.criteriaBuilder.label.criteria" />
      </div>
      <div class="errorMessage"><html-el:errors/></div>

      <html:form action="/em/cb/ruleEditStart.do">
        <html:hidden property="fromType" />
        <input type=hidden name=fromCriteria value=true>

        <div class="pageSubtitle">
          <span class="left"><bean:message key="emarketing.criteriaBuilder.label.criteriaConditionList" bundle="em"/></span>
          <span class="right">
            <input type="button" class="inputButton" 
              value='<bean:message bundle="em" key="button.finish"/>'
              onClick="doActionFinish( this.form);" >
          </span>
        </div>
      </html:form>

      <div class="pageInstructions">
        <bean:message bundle="em" key="emarketing.criteriaBuilder.pageInstructions" />
      </div>

  <!--Start main content area--> <!--start list-->
  <table class="dataTable" border="0" cellspacing="1" cellpadding="2" width="100%">
    <tr class="listHeader">
      <th id="condition" align="left" width="85%">
        <bean:message bundle="em" key="emarketing.criteriaBuilder.label.criteriaConditionList" />
      </th>
      <th id="Actions" align="left" width="15%" nowrap>
        <bean:message bundle="em" key="emarketing.criteriaBuilder.label.action"/>
      </th>
    </tr>
    <c:forEach var="oneNode" items="${nodeList}" varStatus="index">
      <c:set var="rowStyle">
        <c:choose>
          <c:when test="${index.count == 1}">
            criteriaHeader
          </c:when>
          <c:when test="${(oneNode.level + 1) % 2 == 0}">
            listEven
          </c:when>
          <c:otherwise>
            listOdd
          </c:otherwise>
        </c:choose>
      </c:set>
      <tr class="<c:out value="${rowStyle}" />" >
        <td width="85%" align="left">
          <c:if test="${oneNode.level > 0}">
            <c:forEach begin="1" end="${oneNode.level}">
            <%=INDENT%>
            </c:forEach>
          </c:if>
          <c:if test="${index.count > 1}">
            <!-- Expand/collapse icon -->
            <c:choose>
              <c:when test="${! oneNode.allowsChildren}">
                &nbsp;
              </c:when>
              <c:otherwise>
                <%-- This is a branch attribute. --%>
                <c:choose>
                  <c:when test="${oneNode.expanded}"> 
                    <%-- This branch is expanded already. --%>
                    <html:link page="/em/cb/criteriaBranchToggle.do" paramId="key" paramName="oneNode" paramProperty="id"><c:out value="${collapseImage}" escapeXml="false"/></html:link>
                  </c:when>
                  <c:otherwise>
                     <html:link page="/em/cb/criteriaBranchToggle.do" paramId="key" paramName="oneNode" paramProperty="id"><c:out value="${expandImage}" escapeXml="false"/></html:link>
                  </c:otherwise>
                </c:choose> <%-- end of expanded choose. --%>
              </c:otherwise>
            </c:choose>
          </c:if>
          <c:out value="${oneNode.friendlyDescription}" escapeXml="false"/>
        </td>
        <td width="15%" align="left" nowrap>
          <c:choose>
            <c:when test="${oneNode.allowsChildren}">
              <%if (pageContext.getAttribute("oneNode") instanceof MvRowConditionBranch) {%>
                <c:out value="${transparentImageHalfWidth}"  escapeXml="false" />
              <%} else {%>
                <html:link page="/em/cb/appendBranchCondition.do"
                  paramId="key" paramName="oneNode" paramProperty="id">
                    <c:out value="${createNewImage}"  escapeXml="false" />
                </html:link>
              <%}%>
              <a href="#"  onClick="launchConditionBrowser(<c:out value="${oneNode.id}" />)"><c:out value="${addImage}"  escapeXml="false"/></a>
            </c:when>
            <c:otherwise>
              <c:out value="${transparentImage}"  escapeXml="false" />
            </c:otherwise>
          </c:choose>
          <c:url var="editUrl" value="/em/cb/editCondition.do">
            <c:param name="key" value="${oneNode.id}" />
            <c:param name="actType" value="edit" />
          </c:url>
          <a href="<c:out value="${editUrl}"/>"><c:out value="${editImage}" escapeXml="false" /></a>
          <c:if test="${index.count > 1}">
            <%-- Not the root condition. --%>
            <a onClick="DeleteCondition( '<html:rewrite page="/em/cb/deleteCondition.do" paramId="key" paramName="oneNode" paramProperty="id"/>')"
               href="#"><c:out value="${deleteImage}"  escapeXml="false" /></a>
          </c:if>
        </td>
      </tr>
    </c:forEach>
  </table>
  <!-- end list -->

    </td>
  </tr>
</table>
<!--end zone 5 content area-->

